import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QLineEdit, QTextEdit, QLabel

class FileSearchTool(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('文件搜索工具')
        self.setGeometry(100, 100, 600, 400)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.layout = QVBoxLayout()
        self.central_widget.setLayout(self.layout)

        self.label = QLabel('输入搜索关键词:')
        self.layout.addWidget(self.label)

        self.search_input = QLineEdit()
        self.layout.addWidget(self.search_input)

        self.search_button = QPushButton('搜索')
        self.search_button.clicked.connect(self.search_files)
        self.layout.addWidget(self.search_button)

        self.result_text = QTextEdit()
        self.result_text.setReadOnly(True)
        self.layout.addWidget(self.result_text)

    def search_files(self):
        keyword = self.search_input.text()
        if not keyword:
            self.result_text.setText('请输入搜索关键词')
            return

        self.result_text.clear()
        matches = []
        for root, dirs, files in os.walk('/'):  # 从根目录开始搜索
            for file in files:
                if keyword in file:
                    matches.append(os.path.join(root, file))

        if matches:
            self.result_text.setText('\n'.join(matches))
        else:
            self.result_text.setText('未找到匹配的文件')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = FileSearchTool()
    window.show()
    sys.exit(app.exec_())